package com.slf.engine.threads;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.base.IBaseDao;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IBusService;
import com.slf.engine.services.impl.BusServiceImpl;
import com.slf.engine.utils.ConnectUtil;
import com.slf.engine.utils.DateUtils;
import com.slf.engine.utils.Factory;

public class SendThread extends Thread {

	private static final Logger log = Logger.getLogger(SendThread.class);
	
	private ThreadGroup group;
	public SendThread(ThreadGroup group)
	{
		super(group, "SEND_THREAD" + Thread.currentThread().getId());
		log.log(Priority.INFO, "SEND_THREAD RUN SUCCESS");
	}
	
	public void run()
	{
		IBusService busService = new BusServiceImpl();
		IBaseDao baseDao = (IBaseDao) Factory.getInstantBiz("baseDao");
		while(SysContext.IS_TREAD_RUN)
		{
			Order order = busService.getOneOrder();
			if(order != null)
			{
				try {
					order.setCzrq(DateUtils.strDate("yyyyMMdd"));
					order.setCzsj(DateUtils.strDate("HHmmss"));
					baseDao.modify(order, "hasSendOrder");
					order = busService.doService(order,ConnectUtil.getInStantic());
					if(order != null)
					{
						busService.doBusResult(order);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					log.log(Priority.ERROR, "数据库连接出现问题");
				}
			}else{
				try {
					log.log(Priority.INFO, "当前没有交易订单，3秒钟后重新获取");
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
				}
			}
			
		}
	}
}
